package com.example.visit

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.VisitEntityItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.HttpType
import com.example.base.mvi.UiState
import com.example.visit.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitActivity")
@AndroidEntryPoint
class VisitActivity : BaseActivity<ActivityVisitBinding,VisitIntent,VisitViewModel>() {
    val visitAdapter by lazy { VisitAdapter() }
    override fun getLayout(): Int  = R.layout.activity_visit

    override fun getVM(): Class<VisitViewModel>  = VisitViewModel::class.java

    var status = ""//预约拜访状态  默认查询全部状态
    var key = ""

    override fun initView() {
        binding.visitRv.apply {
            adapter = visitAdapter
            layoutManager = LinearLayoutManager(this@VisitActivity)
        }
        binding.head.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.text = "预约管理"

            headAdd.setOnClickListener {
                ARouter.getInstance().build("/visit/VisitAddActivity").navigation()
            }
        }
        binding.fab.setOnClickListener {
            ARouter.getInstance().build("/visit/VisitAddActivity").navigation()
        }
        binding.visitRg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->status = ""
                R.id.rb2->status = "已通过"
                R.id.rb3->status = "驳回"
            }
            if(status!=""){
                viewModel.sendIntent(VisitIntent.visitStatus(status))
            }else{
                viewModel.sendIntent(VisitIntent.visit)
            }
        }
        binding.visitSv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                key = newText.toString()
                if(key!=""){
                    viewModel.sendIntent(VisitIntent.visitName(key))
                }else{
                    viewModel.sendIntent(VisitIntent.visit)
                }
                return true
            }
        })
    }

    override fun initData() {
        lifecycleScope.launch {
            viewModel.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            HttpType.DEFAULT -> {
                                val items = it.data as Collection<VisitEntityItem>
                                //根据名称查询拜访记录，当名称未查到记录，那么我们执行按访问目的查询
                                if(items.size==0){
                                    viewModel.sendIntent(VisitIntent.visitPurpose(key))
                                }else{
                                    visitAdapter.apply {
                                        data.clear()
                                        addData(items.sortedByDescending { it.id })
                                        notifyDataSetChanged()
                                    }
                                }

                            }

                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }
    override fun onResume() {
        super.onResume()
        if(status!=""){
            viewModel.sendIntent(VisitIntent.visitStatus(status))
        }else{
            viewModel.sendIntent(VisitIntent.visit)
        }

    }

}